'use strict';
var AKONG = window.AKONG || {};
AKONG.AutoPlay = function() {

}
AKONG.AutoPlay.prototype = {
	constructor: AKONG.AutoPlay,
	init: function(obj) {
		var self = this;
		self.scene = obj.scene;
		self.child = self.scene.children;
		self.time = obj.time || 500;
		self.delayTime = obj.delayTime || 2000;
		self.autoplay = obj.autoplay || false;
		self.className = obj.className || 'hide';
		self.activeName = obj.activeName || false;
		self.loop = typeof obj.loop === 'boolean' ? obj.loop : true;
		self.maxIndex = self.child.length - 1;
		self.curIndex = obj.curIndex || 0;
		self.curIndex = Math.min(self.curIndex, self.maxIndex);
		self.prevIndex = self.curIndex;
		self.type = obj.type || 0;
		self.animateName = obj.animateName || [];
		self.orient = obj.orient || 0;
		self.direction = 0;
		self.canPlay = true;
		self.animate_id = null;
		self.state = 0;

		self.start = obj.start || false;
		self.callback = obj.callback || false;

		var transitionEnd = function(e, target) {
				var that = target,
					child = self.child;
				if (self.prevIndex === null) {
					return;
				}
				if (self.type === 0) {
					if (that !== self.scene) {
						return;
					}
				} else if (that !== child[self.curIndex]) {
					return;
				}
				
				if (e.target !== target) {
					return;
				}
				if (self.type === 1) {
					var c1S = child[self.prevIndex].style,
						c2S = child[self.curIndex].style;
					child[self.prevIndex].classList.add(self.className);
					c1S.webkitTransition = '0s';
					c1S.webkitTransform = 'translate3d(0,0,0)';
					c2S.webkitTransition = '0s';
				}
				end();
			},
			animationEnd = function(e, target) {
				var that = target,
					child = self.child;
				if (self.prevIndex === null || child[self.curIndex] !== target) {
					return;
				}
				if (that !== child[self.curIndex]) {
					return;
				}
				if (e.target !== target) {
					return;
				}
				child[self.prevIndex].classList.add(self.className);
				child[self.curIndex].classList.remove(self.animateName[1]);
				child[self.prevIndex].classList.remove(self.animateName[0]);
				child[self.curIndex].classList.remove(self.animateName[3]);
				child[self.prevIndex].classList.remove(self.animateName[2]);
				end();
			},
			end = function() {
				requestAnimationFrame(function() {
					var child = self.child;
					if (self.activeName) {
						child[self.prevIndex].classList.remove(self.activeName);
						child[self.curIndex].classList.add(self.activeName);
					}
					self.canPlay = true;
					if (self.autoplay && self.state) {
						if (self.direction === 1) {
							self.animate_id = setTimeout(function() {
								self.next();
							}, self.delayTime);
						} else {
							self.animate_id = setTimeout(function() {
								self.prev();
							}, self.delayTime);
						}
					}
					if (self.callback) {
						self.callback();
					}
				});
			};
		if (self.type === 0) {
			if (self.activeName) {
				self.child[self.curIndex].classList.add(self.activeName);
			}
			self.scene.style.cssText += '-webkit-backface-visibility: hidden;-webkit-transform:translate3d(0,0,0);-webkit-transition:-webkit-transform ' + self.time + 'ms;';
			self.scene.offsetWidth;
			self.scene.addEventListener('webkitTransitionEnd', function(e) {
				e.stopPropagation();
				transitionEnd.call(self, e, self.scene);
			});
		} else {
			Array.prototype.forEach.call(self.child, function(v, i) {
				if (self.type === 1) {
					v.style.webkitTransition = '0s';
				}
				v.style.webkitTransform = 'translate3d(0,0,0)';
				v.style.webkitBackfaceVisibility = 'hidden';
				if (i !== self.curIndex) {
					v.classList.add(self.className);
				} else {
					v.classList.remove(self.className);
					if (self.activeName) {
						v.classList.add(self.activeName);
					}
				}
				if (self.type === 1) {
					v.addEventListener('webkitTransitionEnd', function(e) {
						e.stopPropagation();
						transitionEnd.call(self, e, v);
					});
				} else if (self.type === 2) {
					v.addEventListener('webkitAnimationEnd', function(e) {
						e.stopPropagation();
						animationEnd.call(self, e, v);
					});
				}
			});
		}

		if (self.autoplay) {
			self.play();
		}
	},
	prev: function() {
		var self = this,
			index = self.curIndex,
			length = self.child.length;
		if (!self.canPlay || (!self.autoplay && !self.loop && index === 0)) {
			return;
		}
		if (self.state) {
			self.pause();
		}
		self.state = 2;
		if (self.start) {
			self.start();
		}
		self.prevIndex = index;
		index--;
		if (index < 0) {
			index += length;
		}
		self.curIndex = index % length;
		self.setIndex();
	},
	next: function() {
		var self = this,
			index = self.curIndex,
			length = self.child.length;
		if (!self.canPlay || (!self.autoplay && !self.loop && index === self.maxIndex)) {
			return;
		}
		if (self.state) {
			self.pause();
		}
		self.state = 1;
		if (self.start) {
			self.start();
		}
		self.prevIndex = index;
		index++;
		self.curIndex = index % length;
		self.setIndex();
	},
	play: function() {
		var self = this;
		self.pause();
		self.direction = 1;
		self.animate_id = setTimeout(function() {
			self.next();
		}, self.delayTime);
	},
	reverse: function() {
		var self = this;
		self.pause();
		self.direction = 2;
		self.animate_id = setTimeout(function() {
			self.prev();
		}, self.delayTime);
	},
	gotoAndPlay: function(index) {
		var self = this;
		self.gotoAndStop(index);
		self.play();
	},
	gotoAndReverse: function(index) {
		var self = this;
		self.gotoAndStop(index);
		self.reverse();
	},
	gotoAndStop: function(index) {
		var self = this;
		if(!self.canPlay){
			return;
		}
		self.pause();
		if (index === self.curIndex) {
			return;
		}
		if (index > self.curIndex) {
			self.state = 1;
		} else {
			self.state = 2;
		}
		if (self.start) {
			self.start();
		}
		if (typeof index === 'number') {
			index = Math.min(index, self.maxIndex);
			self.prevIndex = self.curIndex;
			self.curIndex = index;
		}
		self.setIndex();
	},
	pause: function() {
		var self = this;
		self.state = 0;
		clearTimeout(self.animate_id);
	},
	to: function(index) {
		var self = this,
			child = self.child;
		if (index === self.curIndex || !self.canPlay) {
			return;
		}
		if (self.state) {
			self.pause();
		}
		if (typeof index === 'number') {
			index = Math.min(index, self.maxIndex);
			self.prevIndex = self.curIndex;
			self.curIndex = index;
		}
		if (self.type === 0) {
			var distance = -(self.curIndex / (self.maxIndex + 1) * 100) >> 0,
				next = self.orient ? '0,' + distance + '%,0' : distance + '%,0,0';
			self.scene.style.webkitTransition = '0s';
			self.scene.style.webkitTransform = 'translate3d(' + next + ')';
			self.scene.offsetWidth;
			self.scene.style.webkitTransition = '-webkit-transform ' + self.time + 'ms';
		} else if (self.type === 1) {
			var c1S = child[self.prevIndex].style,
				c2S = child[self.curIndex].style,
				cur = '0,0,0',
				prev = self.orient ? '0,-100%,0' : '-100%,0,0',
				next = self.orient ? '0,100%,0' : '100%,0,0';
			child[self.prevIndex].classList.add(self.className);
			c1S.webkitTransition = '0s';
			c1S.webkitTransform = 'translate3d(0,0,0)';
			c2S.webkitTransition = '0s';
			child[self.curIndex].classList.remove(self.className);
			child[self.curIndex].offsetHeight;
		} else {
			child[self.prevIndex].classList.add(self.className);
			child[self.curIndex].classList.remove(self.className);
		}
		self.canPlay = false;
		requestAnimationFrame(function() {
			if (self.activeName) {
				child[self.prevIndex].classList.remove(self.activeName);
				child[self.curIndex].classList.add(self.activeName);
			}
			self.canPlay = true;
			if (self.autoplay) {
				if (self.direction === 1) {
					self.animate_id = setTimeout(function() {
						self.next();
					}, self.delayTime);
				} else {
					self.animate_id = setTimeout(function() {
						self.prev();
					}, self.delayTime);
				}
			}
			if (self.callback) {
				self.callback();
			}
		});
	},
	setIndex: function() {
		if (!this.canPlay || arguments.length !== 0) {
			return;
		}

		var self = this,
			time = self.time,
			child = self.child,
			state = self.state || (self.curIndex > self.prevIndex ? 1 : 2);
		self.canPlay = false;

		if (self.type === 0) {
			var distance = -(self.curIndex / (self.maxIndex + 1) * 100) >> 0,
				next = self.orient ? '0,' + distance + '%,0' : distance + '%,0,0';
			self.scene.style.webkitTransform = 'translate3d(' + next + ')';
		} else if (self.type === 1) {
			var c1S = child[self.prevIndex].style,
				c2S = child[self.curIndex].style,
				cur = '0,0,0',
				prev = self.orient ? '0,-100%,0' : '-100%,0,0',
				next = self.orient ? '0,100%,0' : '100%,0,0';
			if (state === 1) {
				c2S.webkitTransform = 'translate3d(' + next + ')';
			} else {
				c2S.webkitTransform = 'translate3d(' + prev + ')';
			}
			child[self.curIndex].classList.remove(self.className);
			child[self.curIndex].offsetHeight;
			requestAnimationFrame(function() {
				if (state === 1) {
					c1S.webkitTransition = '-webkit-transform ' + time + 'ms';
					c1S.webkitTransform = 'translate3d(' + prev + ')';
					c2S.webkitTransition = '-webkit-transform ' + time + 'ms';
					c2S.webkitTransform = 'translate3d(' + cur + ')';
				} else {
					c1S.webkitTransition = '-webkit-transform ' + time + 'ms';
					c1S.webkitTransform = 'translate3d(' + next + ')';
					c2S.webkitTransition = '-webkit-transform ' + time + 'ms';
					c2S.webkitTransform = 'translate3d(' + cur + ')';
				}
			});
		} else {
			child[self.curIndex].classList.remove(self.className);
			if (self.animateName.length === 2 || state === 1) {
				child[self.curIndex].classList.add(self.animateName[1]);
				child[self.prevIndex].classList.add(self.animateName[0]);
			} else {
				child[self.curIndex].classList.add(self.animateName[3]);
				child[self.prevIndex].classList.add(self.animateName[2]);
			}
		}
	}
}